package com.mallcai.bigdata.ladon.dpl.mapper.peekdata.provider;

import com.mallcai.bigdata.ladon.dpl.entity.peekdata.Peek;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import com.mallcai.bigdata.ladon.dpl.mapper.system.UserMapper;
import com.mallcai.bigdata.ladon.dpl.mapper.system.provider.UserSqlProvider;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
public class PeekSqlProvider extends BaseSqlProvider<Peek> {
    public static final String TB_PEEK = "pd_peek";


    @Override
    public String getTableName() {
        return TB_PEEK;
    }

    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("model_id", "#{modelId}");
        map.put("name", "#{name}");
        map.put("fields", "#{fields}");
        map.put("peek_time", "#{peekTime}");
        return map;
    }

    @Override
    public Map<String, String> getUpdateColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("modelId", "model_id");
        map.put("name", "name");
        map.put("fields", "fields");
        map.put("peekTime", "peek_time");
        return map;
    }

    /**
     * 根据参数查询
     */
    public String getByParams(Map<String, Object> map) {
        Map<String, Object> params = getParams(map);

        StringBuilder sb = new StringBuilder();
        sb.append("tb_p.archive = 0");
        if (params.containsKey("name") && StringUtils.isNotBlank(params.get("name").toString())) {
            sb.append(" and tb_p.name like concat('%', #{params.name}, '%')");
        }
        if (params.containsKey("fields") && StringUtils.isNotBlank(params.get("fields").toString())) {
            sb.append(" and tb_p.fields like concat('%', #{params.fields}, '%')");
        }
        if (params.containsKey("modelId")) {
            sb.append(" and tb_p.model_id = #{params.modelId}");
        }
        if (params.containsKey("createdBy")) {
            sb.append(" and tb_p.created_by = #{params.createdBy}");
        }
        if (params.containsKey("peekName") && StringUtils.isNotBlank(params.get("peekName").toString())) {
            sb.append(" and tb_p.name = #{params.peekName} ");
        }

        if (params.containsKey("newVersion") && StringUtils.isNotBlank(params.get("newVersion").toString())) {
            sb.append(" and tb_p.fields is null");
        } else {
            sb.append(" and tb_p.fields is not null");
        }
        return new SQL() {
            {
                SELECT("tb_p.*, tb_m.name AS model_name, tb_u.nickname AS creator");
                LEFT_OUTER_JOIN(ModelSqlProvider.TB_MODEL + " tb_m  ON tb_m.id = tb_p.model_id ");
                LEFT_OUTER_JOIN(UserSqlProvider.TB_USER + " tb_u ON tb_u.id = tb_p.created_by ");
                FROM(TB_PEEK + " tb_p");
                WHERE(sb.toString());
            }
        }.toString();
    }

    /**
     * 根据Id查询
     */
    public String getById(@Param("id") Long id) {
        return new SQL() {
            {
                SELECT("*");
                FROM(TB_PEEK);
                WHERE("id = #{id}");
            }
        }.toString();
    }
}
